 //
//  ComposeViewController.swift
//  DSWeibo
//
//  Created by J on 16/5/5.
//  Copyright © 2016年 J. All rights reserved.
//

import UIKit
import SVProgressHUD

class ComposeViewController: UIViewController {

    
    override func viewDidLoad() {
        //1.初始化导航条
        setupNav()
        
        //2.初始化输入框
        setupInputView()
    }
    
    //1.初始化导航条
    func setupNav(){
        view.backgroundColor = UIColor.whiteColor()
        //1.添加左边按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(close))
        //2.添加右边按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(sendStatus))
        navigationItem.rightBarButtonItem?.enabled = false
        
        //3.添加中间视图
        let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 32))
        let label1 = UILabel()
        label1.text = "发送微博"
        label1.font = UIFont.systemFontOfSize(13)
        label1.sizeToFit()
        titleView.addSubview(label1)
        
        let label2 = UILabel()
        label2.text = UserAccount.loadAccount()?.screen_name
        label2.font = UIFont.systemFontOfSize(13)
        label2.sizeToFit()
        titleView.addSubview(label2)
        
        navigationItem.titleView = titleView
        
        label1.snp_makeConstraints { (make) in
            make.top.equalTo(titleView)
            make.centerX.equalTo(titleView)
        }
        
        label2.snp_makeConstraints { (make) in
            make.bottom.equalTo(titleView)
            make.centerX.equalTo(titleView)
        }
        
        
    }
    
    
    
    private func setupInputView(){
        view.addSubview(textView)//添加子控件
        textView.snp_makeConstraints { (make) in
            make.edges.equalTo(view)
        }
        
        
        textView.addSubview(placeholderLabel)
        placeholderLabel.snp_makeConstraints { (make) in
            make.left.equalTo(textView).offset(5)
            make.top.equalTo(textView).offset(8)
        }
        
    }
    
    private lazy var placeholderLabel :UILabel = {
        let lb = UILabel()
        lb.font = UIFont.systemFontOfSize(12)
        lb.textColor = UIColor.darkGrayColor()
        lb.text = "分享新鲜事......"
        return lb
    }()
    //MARK: --懒加载
    
    private lazy var textView: UITextView = {
        let tv = UITextView()
        tv.delegate = self
        return tv
        
    }()
    
    func close(){//关闭控制器
       
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    
    
    func sendStatus(){//发送微博
        
        let path = "2/statuses/update.json"
        
        
        let params = ["access_token":UserAccount.account?.access_token!,"status":textView.text]
        

        NetWorkTools.shareNetWorkTools().POST(path, parameters: params, success: { (_, result:AnyObject?) in
            
            print(result)
            //提示用户发送成功关闭发送界面
            
            SVProgressHUD.showSuccessWithStatus("发送成功")
            self.close()
        }) { (_, error:NSError) in
            
            //提示用户发送失败
            print(error)
            
            SVProgressHUD.showErrorWithStatus("发送失败")

        }
        
        
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        textView.becomeFirstResponder()
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        textView.resignFirstResponder()
    }
    
}
 
 extension ComposeViewController:UITextViewDelegate{
    func textViewDidChange(textView: UITextView) {
        placeholderLabel.hidden = textView.hasText()
        navigationItem.rightBarButtonItem?.enabled = textView.hasText()
        
        
    }
    
 }
